﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports Microsoft.Samples.WindowsPhoneCloud.Phone.ViewModel
Imports Microsoft.Phone.Tasks

Namespace Pages
    Partial Public Class UploadPhotoPage
        Inherits PhoneApplicationPage
        Public Sub New()
            Me.InitializeComponent()

            Me.ViewModel = New UploadPhotoPageViewModel()
        End Sub

        Public Property ViewModel() As UploadPhotoPageViewModel
            Get
                Return TryCast(Me.DataContext, UploadPhotoPageViewModel)
            End Get
            Set(ByVal value As UploadPhotoPageViewModel)
                Me.DataContext = value
            End Set
        End Property

        Protected Overrides Sub OnNavigatedTo(ByVal e As NavigationEventArgs)
            Dim photoResult = GetApplicationState(Of PhotoResult)("PhotoResult")
            If (Me.ViewModel IsNot Nothing) AndAlso (photoResult IsNot Nothing) Then
                Me.ViewModel.LoadPhoto(photoResult)
            Else
                Me.NavigationService.GoBack()
            End If
        End Sub

        Private Shared Function GetApplicationState(Of T)(ByVal key As String) As T
            If Not PhoneApplicationService.Current.State.ContainsKey(key) Then
                Return Nothing
            End If

            Return CType(PhoneApplicationService.Current.State(key), T)
        End Function

        Private Sub OnUploadPicture(ByVal sender As Object, ByVal e As RoutedEventArgs)
            If Me.ViewModel IsNot Nothing Then
                If String.IsNullOrEmpty(Me.ViewModel.BlobName) Then
                    MessageBox.Show("The blob name cannot be null nor empty.", "Upload Photo Result", MessageBoxButton.OK)
                Else
                    Try
                        Me.ViewModel.UploadPhoto(
                            Sub(successMessage)
                                Me.Dispatcher.BeginInvoke(
                                    Sub()
                                        MessageBox.Show(successMessage, "Upload Photo Result", MessageBoxButton.OK)
                                        PhoneApplicationService.Current.State.Remove("PhotoResult")
                                        Me.NavigationService.GoBack()
                                    End Sub)
                            End Sub,
                            Sub(errorMessage)
                                Me.Dispatcher.BeginInvoke(
                                    Sub() MessageBox.Show(errorMessage, "Upload Photo Result", MessageBoxButton.OK))
                            End Sub)
                    Catch ex As Exception
                        MessageBox.Show(ex.Message, "Upload Photo Result", MessageBoxButton.OK)
                        Me.NavigationService.GoBack()
                    End Try
                End If
            End If
        End Sub
    End Class

End Namespace